package com.xiaocikuai.smallchunks.controller.Advertise;

//import com.xiaocikuai.smallchunks.entity.Advertise.Advertise;
//import com.xiaocikuai.smallchunks.service.Advertise.AdvertiseService;
//import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.util.Date;
//
//@RestController
//@RequestMapping("/api/advertise")
//@CrossOrigin
//public class AdvertiseController {
//
//    @Autowired
//    private AdvertiseService advertiseService;
//
//    //增加广告
//    @PostMapping("/add")
//    public Resultd AddAdvertise(@RequestHeader("Authorization") String token,
//                                @RequestParam("file") MultipartFile file,
//                                @RequestParam("theme") String theme,
//                                    @RequestParam("url") String url,
//                                @RequestParam("time") Date time,
//                                @RequestParam("platform") Integer platform )
//    {
//        advertiseService.addAdvertise(file,theme,url,time,platform);
//         return Resultd.ok().data("info","添加成功");
//    }
//
//    //删除广告
//    @PostMapping("/Delete")
//    public Resultd DeleteAdvertise(@RequestHeader("Authorization") String token, @RequestBody Advertise advertise)
//    {
//        advertiseService.DeleteAdvertise(advertise);
//        return Resultd.ok().data("info","删除成功");
//    }
//
//    //更新广告
//    @PostMapping("/update")
//    public Resultd UpdateAdvertise(@RequestHeader("Authorization") String token,
//                                @RequestParam("file") MultipartFile file,
//                                @RequestParam("theme") String theme,
//                                @RequestParam("url") String url,
//                                @RequestParam("time") Date time,
//                                @RequestParam("platform") Integer platform )
//    {
//         advertiseService.UpdateAdvertise(file,theme,url,time,platform);
//        return Resultd.ok().data("info","更新成功");
//    }
//
///*    @PostMapping("/list")
//    public Resultd ListAdvertise(@RequestHeader("Authorization") String token, @RequestBody Advertise advertise)
//    {
//        advertiseService.UpdateAdvertise(advertise);
//        return Resultd.ok().data("info","更新成功");
//    }
//    */
//
//}
import com.xiaocikuai.smallchunks.entity.Advertise.Advertise;
import com.xiaocikuai.smallchunks.entity.Advertise.ImgUni;
import com.xiaocikuai.smallchunks.service.Advertise.AdvertiseService;
import com.xiaocikuai.smallchunks.service.Advertise.impl.ImgUniServiceImpl;
import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

@Api(tags = "广告管理控制器")
@RestController
@RequestMapping("/api/advertise")
@CrossOrigin
public class AdvertiseController {

    @Autowired
    private AdvertiseService advertiseService;

    @Autowired
    private ImgUniServiceImpl imgUniService;

    @ApiOperation(value = "增加广告")
    @PostMapping("/add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "file", value = "广告文件", required = true, dataType = "MultipartFile", paramType = "form"),
            @ApiImplicitParam(name = "theme", value = "广告主题", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "url", value = "广告链接", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "time", value = "广告时间", required = true, dataType = "Date", paramType = "form"),
            @ApiImplicitParam(name = "platform", value = "广告平台", required = true, dataType = "Integer", paramType = "form")
    })
    public Resultd AddAdvertise(@RequestHeader("Authorization") String token,
                                @RequestParam("file") MultipartFile file,
                                @RequestParam("theme") String theme,
                                @RequestParam("url") String url,
                                @RequestParam("time") Date time,
                                @RequestParam("platform") Integer platform )
    {
        advertiseService.addAdvertise(file,theme,url,time,platform);
        return Resultd.ok().data("info","添加成功");
    }

    @ApiOperation(value = "删除广告")
    @PostMapping("/Delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "advertise", value = "广告实体", required = true, dataType = "Advertise", paramType = "body")
    })
    public Resultd DeleteAdvertise(@RequestHeader("Authorization") String token, @RequestBody Advertise advertise)
    {
        advertiseService.DeleteAdvertise(advertise);
        return Resultd.ok().data("info","删除成功");
    }

    @ApiOperation(value = "更新广告")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "授权令牌", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "file", value = "广告文件", required = true, dataType = "MultipartFile", paramType = "form"),
            @ApiImplicitParam(name = "theme", value = "广告主题", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "url", value = "广告链接", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "time", value = "广告时间", required = true, dataType = "Date", paramType = "form"),
            @ApiImplicitParam(name = "platform", value = "广告平台", required = true, dataType = "Integer", paramType = "form")
    })
    public Resultd UpdateAdvertise(@RequestHeader("Authorization") String token,
                                   @RequestParam("file") MultipartFile file,
                                   @RequestParam("theme") String theme,
                                   @RequestParam("url") String url,
                                   @RequestParam("time") Date time,
                                   @RequestParam("platform") Integer platform )
    {
        advertiseService.UpdateAdvertise(file,theme,url,time,platform);
        return Resultd.ok().data("info","更新成功");
    }


    @ApiOperation(value = "上传轮播图")
    @PostMapping("/AddImgs")
    public Resultd AddImgs(@RequestHeader("Authorization") String token, @RequestParam("file") MultipartFile file, ImgUni imgUni) {

        List<ImgUni> list = imgUniService.selectByType(imgUni.getType());

        if(list==null || list.size()==0){
            return Resultd.error().data("info", "添加失败");
        } else {
            for (ImgUni r : list) {
                if(!imgUniService.updateList(r,file))
               {
                 return Resultd.error().data("info", "添加失败");
               }
            }
        return Resultd.ok().data("info", "添加成功");
        }


    }

    @PostMapping("/getImg")
    public Resultd getImg(@RequestHeader("Authorization") String token,@RequestBody Advertise advertise) {
        Advertise returns = advertiseService.getImg(advertise.getPlatform());
        System.out.println(advertise.getPlatform());
        if (returns != null){
            return Resultd.ok().data("info", returns);
        } else {
            return Resultd.error().data("info","获取广告失败");
        }

    }




}